using UnityEngine;
using System.Collections;

public class BM_BaseToy : BM_LevelObject
{
	public enum Toy
	{
		TOY_NONE = -1

	,	TOY_SWORD
	,	TOY_SHIELD
	,	TOY_HELM
	,	TOY_ROBELLO
	,	TOY_ANDY
	,	TOY_DRAGON

	,	TOY_COUNT
	}

	protected enum State
	{
		STATE_NONE = -1

	,	STATE_IDLE
	,	STATE_CUT
	,	STATE_ACTION

	,	STATE_COUNT
	}


	#region Members

	public Toy			ToyName = Toy.TOY_NONE;
	public MessageID	ActivationMessage = MessageID.MSGID_NONE;
	public GameObject	RealObject = null;
	public GameObject	FakeObject = null;
	public GameObject	ParticleObj = null;

	public bool				m_bQuestActive = false;

	protected player_script		m_rPlayer = null;

	private float		m_fCutDelayTime = 0.3f;
	private float		m_fCutChangeTime = 0.5f;
	private float		m_fCutActionTime = 1.5f;
	private float		m_fCutDelayTimeStamp = 0.0f;
	private float		m_fCutChangeTimeStamp = 0.0f;
	private float		m_fCutActionTimeStamp = 0.0f;

	protected GameObject	m_rCurrentObject = null;

	public Transform	selector;
	protected bool	 	m_bSelectable 	= true;

	private AudioClip	m_rTransAudioClip = null;

	#endregion


	public bool QuestActive
	{
		get { return m_bQuestActive; }
	}

	public bool Selectable
	{
		get { return m_bSelectable; }
	}

	public override void Awake ()
	{
		base.Awake ();

		GameObject rPlayerObject = GameObject.FindGameObjectWithTag(BM_Common.GetLabelName(StringLabel.LABEL_TAG_PLAYER));
		if(rPlayerObject != null)
			m_rPlayer = rPlayerObject.GetComponent<player_script>();

		RealObject.SetActive(true);
		FakeObject.SetActive(false);
		m_rCurrentObject = RealObject;

		if(selector)
			selector.gameObject.renderer.enabled = false;

		m_rTransAudioClip = Resources.Load("sfx_trans") as AudioClip;
	}


	public override void Update ()
	{
		base.Update ();


		BM_BaseMessage rBaseMsg = m_rMessageHandler.ReceivedMessageWithID(ActivationMessage);
		if(rBaseMsg != null)
		{
			if(rBaseMsg.m_eMessageID > MessageID.MSGID_QUEST_START && rBaseMsg.m_eMessageID < MessageID.MSGID_QUEST_END)
				m_bQuestActive = ((BM_QuestActivationMessage)rBaseMsg).m_bActive;
				m_bSelectable = m_bQuestActive;
		}

		rBaseMsg = m_rMessageHandler.ReceivedMessageWithID(MessageID.MSGID_TOY_PERFORM_SELECTION);
		if(rBaseMsg != null)
		{
			// Check name
			if(((BM_ToySelectionMessage)rBaseMsg).m_eSelectedToyName == ToyName && m_rFSM.CurrentState == (int)State.STATE_IDLE && m_bSelectable) 
			{
				selector.gameObject.renderer.enabled = true;
				selector.up = Vector3.forward;
			} else {
				selector.gameObject.renderer.enabled = false;
			}
		}
	}


	protected void InitializeCutState()
	{
		m_fCutDelayTimeStamp = Time.time+m_fCutDelayTime;
		m_fCutChangeTimeStamp = Time.time+m_fCutChangeTime;
		m_fCutActionTimeStamp = Time.time+m_fCutActionTime;

		m_rMessageHandler.SendMessageToAllButMe(new BM_BaseMessage(MessageID.MSGID_STOP_INTERACTION, MessageHandler));
	}
	
	protected void UpdateCutState()
	{
		if(Time.time >= m_fCutDelayTimeStamp)
		{
			if(ParticleObj)
				ParticleObj.particleSystem.Play();
			m_fCutDelayTimeStamp = float.MaxValue;

			AudioSource.PlayClipAtPoint(m_rTransAudioClip, Vector3.zero, 0.2f);
		}

		if(Time.time >= m_fCutChangeTimeStamp)
		{
			RealObject.SetActive(false);
			FakeObject.SetActive(true);
			m_rCurrentObject = FakeObject;

			m_fCutChangeTimeStamp = float.MaxValue;
		}

		if(Time.time >= m_fCutActionTimeStamp)
		{
			m_rFSM.NextState = (int)State.STATE_ACTION;
			m_rMessageHandler.SendMessageToAllButMe(new BM_BaseMessage(MessageID.MSGID_START_INTERACTION, MessageHandler));
			
			m_fCutActionTimeStamp = float.MaxValue;
		}
	}
}
